//
//  PictureViewController.m
//  RecreationGame
//
//  Created by appying_wxj on 15/1/27.
//  Copyright (c) 2015年 appying_wxj. All rights reserved.
//

#import "PictureViewController.h"
#import <MobileCoreServices/UTCoreTypes.h>
@interface PictureViewController ()

@end

@implementation PictureViewController
@synthesize lastChosenMediaType;
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.titleLab.text = @"照片界面";
    
    [self.leftButton addTarget:self action:@selector(leftButtonClick) forControlEvents:UIControlEventTouchUpInside];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.view.userInteractionEnabled = YES;
    [self initPictureFinish];
}

// 创建拍照的照片
-(void)initPictureFinish{
    
    _chooseImage = [[UIImageView alloc] init];
    _chooseImage.frame = CGRectMake(30, 80, kwidth-60, kwidth-60);
    _chooseImage.backgroundColor = [UIColor colorWithRed:0.67 green:0.8 blue:0.35 alpha:1];
    _chooseImage.userInteractionEnabled = YES;
     _chooseImage.image=_chosenImage;
    [self.view addSubview:_chooseImage];
    
    UIButton *chooseButton = [UIButton buttonWithType:UIButtonTypeCustom];
    chooseButton.backgroundColor = [UIColor clearColor];
    chooseButton.frame = CGRectMake(30, 80, kwidth-60, kwidth-60);
    [chooseButton addTarget:self action:@selector(chooseButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:chooseButton];
    
    UITextView *contentText = [[UITextView alloc] init];
    contentText.frame = CGRectMake(10, 100+kwidth-60, kwidth-20, 100);
    contentText.backgroundColor = [UIColor colorWithRed:0.67 green:0.8 blue:0.35 alpha:0.8];
    [self.view addSubview:contentText];

    UIButton *shareButton = [UIButton buttonWithType:UIButtonTypeCustom];
    shareButton.frame = CGRectMake(10, kHeight-80, kwidth-20, 60);
    [shareButton setTitle:@"分享" forState:UIControlStateNormal];
    [self.view addSubview:shareButton];
}
-(void)chooseButtonClick{
    [self shootPiicturePrVideo];
}
//从相机上选择
-(void)shootPiicturePrVideo{
    [self getMediaFromSource:UIImagePickerControllerSourceTypeCamera];
}
// 拍照
-(void)getMediaFromSource:(UIImagePickerControllerSourceType)sourceType{
    
    NSArray *mediatypes=[UIImagePickerController availableMediaTypesForSourceType:sourceType];
    if([UIImagePickerController isSourceTypeAvailable:sourceType] &&[mediatypes count]>0){
        NSArray *mediatypes=[UIImagePickerController availableMediaTypesForSourceType:sourceType];
        UIImagePickerController *picker=[[UIImagePickerController alloc] init];
        picker.videoQuality = UIImagePickerControllerQualityTypeHigh;
        picker.mediaTypes=mediatypes;
        picker.delegate=self;
        picker.allowsEditing=NO;// 设置不可编辑
        picker.sourceType=sourceType;
        NSString *requiredmediatype=(NSString *)kUTTypeImage;
        NSArray *arrmediatypes=[NSArray arrayWithObject:requiredmediatype];
        [picker setMediaTypes:arrmediatypes];
        [self presentViewController:picker animated:YES completion:nil];
    }
    else{
        UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"错误信息!"
                                                      message:@"当前设备不支持拍摄功能"
                                                     delegate:nil
                                            cancelButtonTitle:@"确认"
                                            otherButtonTitles: nil];
        [alert show];
    }
    
}
// 拍照模块
//点击相册中的图片或照相机照完后点击use  后触发的方法
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:
(NSDictionary *)info{
    self.lastChosenMediaType=[info objectForKey:UIImagePickerControllerMediaType];
    if([lastChosenMediaType isEqual:(NSString *) kUTTypeImage])
    {
        _chosenImage=[info objectForKey:UIImagePickerControllerEditedImage];
//        _chooseImage.image=_chosenImage;
        NSLog(@"----照片-------%@",_chooseImage.image);
    }
    if([lastChosenMediaType isEqual:(NSString *) kUTTypeMovie])
    {
        UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"提示信息!"
                                                      message:@"系统只支持图片格式"
                                                     delegate:nil
                                            cancelButtonTitle:@"确认"
                                            otherButtonTitles: nil];
        [alert show];
        
    }
    
    [picker dismissViewControllerAnimated:YES completion:nil];

}
-(void)leftButtonClick{
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
